package com.cictec.middleware.gps.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.Result;
import com.cictec.middleware.commons.utils.ResultUtil;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.BusInstanceEngine;
import com.cictec.middleware.gps.service.BusDriverService;
import com.cictec.middleware.ribbon.loadbalancer.rule.ThreadContextHolder;

/**
 * 中间件GPS服务司机相关业务
 * 
 * @author Administrator
 *
 */
@RestController
public class MiddlewareDriverController {

	private static Logger logger = LoggerFactory.getLogger(MiddlewareDriverController.class);

	@Autowired
	private BusDriverService busDriverService;
	@Autowired
	private BusInstanceEngine busInstanceEngine;

	@RequestMapping(value = "/gps/busDriver/refreshDriverSignIn", method=RequestMethod.POST)
	public Result refreshBusDriver(@RequestParam("devCode") String devCode,
			@RequestParam("icCardNumber") String icCardNumber, @RequestParam("driverSignTime") Date driverSignTime) {
		System.out.println("签到时间："+ DateUtils.formatDateTime(driverSignTime));
		BusInstance busInstance = busInstanceEngine.getBusInstanceByDevCode(devCode);
		ThreadContextHolder.setValue(devCode, busInstance.getDevice().getCityCode());// 目前先通过代码侵入方式实现，后期在优化
		busDriverService.refreshDriverSignIn(busInstance.getBus().getBusUuid(), DateUtils.formatDate(driverSignTime));

		String msg = "刷新GPS服务司机事件";
		logger.debug(msg);
		return ResultUtil.getSuccessResult(msg);
	}

}
